/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.mvc.views
{
	import com.puny.mvc.MvcEvent;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	
	/**
	 * Dispatched when a state of the instance becomes selected.
	 * @eventType com.puny.mvc.MvcEvent.SELECTED
	 */
	[Event(name="selected", type="com.puny.mvc.MvcEvent")]
	/**
	 * Dispatched when a state of the instance becomes unselected.
	 * @eventType com.puny.mvc.MvcEvent.UNSELECTED
	 */
	[Event(name="unselected", type="com.puny.mvc.MvcEvent")]
	
	/**
	 * A view class for a toggle buttons. Toggle Button is switching two states, selected and unselected. For example, a check box is a toggle button. A sound mute button is another example.
	 * Each state should be a button. In the other words, Toggle Button consists of selected button and unselected one.
	 * @author puny
	 * @example
	 * "MyAssets.swf" should have two exported button assets, "SoundOn2OffButton" and "SoundOff2OnButton". In this case, button assets should have button structure for this framework.
	 * <listing version="3.0">
	 * var _muteButotn:ToggleButtonView;
	 * _muteButton =addChild(new ToggleButtonView("MyAssets.swf", "SoundOn2OffButton", "SoundOff2OnButton", true)) as ToggleButtonView;
	 * _muteButton.addToggleListeners(off2onHandler, on2offHandler);
	 * 
	 * private function off2onHandler(event:Evnet):void {
	 * 	//make sound on
	 * }
	 * 
	 * private function on2offHandler(event:Event):void {
	 * 	//make sound off
	 * }

	 * </listing>
	 * @see com.puny.mvc.views.ButtonView
	 */
	public class ToggleButtonView extends ButtonView {
		
		private var _selectedMC:MovieClip;
		private var _unselectedMC:MovieClip;
		private var _selected:String;
		private var _unselected:String;
		private var _selectedEvent:Event = new Event(MvcEvent.SELECTED);
		private var _unselectedEvent:Event = new Event(MvcEvent.UNSELECTED);
		private var _initialState:Boolean;
		/**
		 * Constructor
		 * @param _swf swf instance or swf class name
		 * @param selected exported name for selected asset in the swf. It should be a button and "out" state should have a selected image.
		 * @param unselected exported name for unselected asset in the swf. It should be a button and "out" state should have an unselected image
		 * @param initial if <code>true</code>, initially the button is selected. <code>false</code> is default.
		 * 
		 */
		public function ToggleButtonView(_swf:Object,selected:String,unselected:String, initial:Boolean=false) {
			_selected = selected;
			_unselected = unselected;
			_initialState = initial;
			super(_swf);
		}
		
		/**
		 * 
		 * @private
		 */
		override protected function setup():void {
			_selectedMC = getMovieClip(_selected);
			_unselectedMC = getMovieClip(_unselected);
			_selectedMC.visible = _initialState;
			_unselectedMC.visible = !_initialState;
			addButtonListeners(_selectedMC, unselectHandler,false,true);
			addButtonListeners(_unselectedMC, selectHandler,false,true);
		}
		
		private function unselectHandler(event:Event):void {
			_selectedMC.visible = false;
			_unselectedMC.visible = true;
			dispatchEvent(_unselectedEvent.clone());	
		}
		
		private function selectHandler(event:Event):void {
			_selectedMC.visible = true;
			_unselectedMC.visible = false;
			dispatchEvent(_selectedEvent.clone());
		}
		
		
		/**
		 * add event listeners for selecting and unselecting. 
		 * @param selectedHandler event handler. This can be <code>null</code>. This happens when an unselected button is clicked.
		 * @param unselectedHandler event handler. This can be <code>null</code>. This happens when a selected button is clicked.
		 */
		public function addToggleListeners(selectedHandler:Function,unselectedHandler:Function):void {
			if (unselectedHandler!=null) {
				addEventListener(MvcEvent.UNSELECTED, unselectedHandler);
			}
			if (selectedHandler!=null) {
				addEventListener(MvcEvent.SELECTED, selectedHandler);
			}
		}
		
		/**
		 * Removing event listetenrs for buttons.
		 * 
		 */
		override public function afterClose():void {
			super.afterClose();
			removeButtonListeners(_selectedMC);
			removeButtonListeners(_unselectedMC);
			_selectedMC = null;
			_unselectedMC = null;
		}
	}
}